from extensions import db
import datetime
from sqlalchemy import Integer


# class InstanceTag(db.Model):
#     __tablename__ = 'sql_instance_tag'
#     """实例标签配置"""
#
#     tag_code = db.Column(db.String(50), comment="标签代码", unique=True)
#     tag_name = db.Column(db.String(50), comment="标签名称", unique=True)
#     active = db.Column(db.Boolean(), comment="激活状态", default=True)
#     create_time = db.Column(db.DateTime(), comment="创建时间", default=datetime.datetime.now)
#
#     def __str__(self):
#         return self.tag_name


class Instance(db.Model):
    """
    各个线上实例配置
    """
    __tablename__ = 'sql_instance'
    __table_args__ = {'extend_existing': True}
    id = db.Column(Integer, comment='id', primary_key=True, )
    instance_name = db.Column(db.String(50), comment="实例名称", unique=True)
    # type = models.CharField(
    #     "实例类型", max_length=6, choices=(("master", "主库"), ("slave", "从库"))
    # )
    db_type = db.Column(db.String(50), comment="数据库类型")
    host = db.Column(db.String(50), comment="实例连接")
    port = db.Column(db.String(50), comment="端口", default=0)
    user = db.Column(db.String(50), comment="用户名", default="")
    password = db.Column(db.String(50), comment="密码", default="")
    db_name = db.Column(db.String(50), comment="数据库", default="")
    charset = db.Column(db.String(50), comment="字符集", default="")
    create_time = db.Column(db.DateTime(), comment="创建时间", default=datetime.datetime.now)
    update_time = db.Column(db.DateTime(), comment="更新时间", default=datetime.datetime.now)


class QueryLog(db.Model):
    """
    记录在线查询sql的日志
    """
    __tablename__ = "query_log"
    __table_args__ = {'extend_existing': True}
    id = db.Column(Integer, comment='id', primary_key=True, autoincrement=True)
    instance_name = db.Column(db.String(50), comment="实例名称")
    db_name = db.Column(db.String(50), comment="数据库名称")
    sqllog = db.Column(db.Text, comment="执行的查询语句")
    query_status = db.Column(db.Integer, comment="查询状态")
    query_message = db.Column(db.Text, comment="查询报错信息")
    effect_row = db.Column(db.Integer, comment="返回行数")
    username = db.Column(db.String(50), comment="操作人")
    create_time = db.Column(db.DateTime, comment="操作时间", default=datetime.datetime.utcnow)


if __name__ == '__main__':
    from app import app

    with app.app_context():
        db.create_all()
